package com.kgcx.push.schedule;

import com.kgcx.common.utils.CollectionUtils;
import com.kgcx.push.entity.*;
import com.kgcx.push.service.*;
import com.kgcx.push.utils.FtpUtil;
import com.kgcx.push.utils.XmlUtil;
import com.xiaoleilu.hutool.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2018/5/10 10:11
 * @since 0.1
 */
@Configuration
@EnableScheduling
@Slf4j
public class JobTask {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private PayService payService;
    @Autowired
    private PositionService positionService;

    @Scheduled(cron = "0/5 * * * * ? ") // 5s执行一次
    public void uploadPoliceData() {
        log.info("driverRedisDel定时任务启动");
        System.out.println(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // 存储十个表日志数据
        // dataSetMap以DATASET为key,dataSetList为value,表示<MESSAGE>标签下十个日志表的<DATASET>标签
        Map<String, Object> dataSetMap = new LinkedHashMap<>();
        List<Map<String, Object>> dataSetList = new ArrayList<>();

        boolean hasData = false;
        //   顾客注册日志信息处理
        List<CustomerLog> cusList = customerService.selectList();
        if (CollectionUtils.isNotEmpty(cusList)) {
            List<Map<String, Object>> customerLoglist = XmlUtil.setCustomerList(cusList);
            // 将转换后的map添加到dataSetList中

            Map customerLogDataSet = XmlUtil.mapToMapForCustomer(customerLoglist);
            dataSetList.add(customerLogDataSet);

            hasData = true;
        }
        // 专车/顺风车/快车注册日志信息处理
        List<DriverLog> driList = driverService.selectList();
        if (CollectionUtils.isNotEmpty(driList)) {
            List<Map<String, Object>> driverLoglist = XmlUtil.setDriverList(driList);
            // 将转换后的map添加到dataSetList中

            Map driverLogDataSet = XmlUtil.mapToMapForDriver1(driverLoglist);
            dataSetList.add(driverLogDataSet);

            hasData = true;
        }
        // 出租车注册日志信息处理
        // 将转换后的map添加到dataSetList中
        //Map driver2DataSet = XmlUtil.mapToMapForDriver2();
        //dataSetList.add(driver2DataSet);

        // 代驾注册日志信息处理
        // 将转换后的map添加到dataSetList中
        // Map driver3DataSet = XmlUtil.mapToMapForDriver3();
        //dataSetList.add(driver3DataSet);

        // 登录日志信息处理
        List<LoginLog> logList = loginService.selectList();
        if (CollectionUtils.isNotEmpty(logList)) {
            List<Map<String, Object>> loginLoglist = XmlUtil.setLoginList(logList);
            // 将转换后的map添加到dataSetList中

            Map loginLogDataSet = XmlUtil.mapToMapForLogin(loginLoglist);
            dataSetList.add(loginLogDataSet);

            hasData = true;
        }
        // 订单日志信息处理
        List<OrderLog> ordList = orderService.selectList();
        if (CollectionUtils.isNotEmpty(ordList)) {
            List<Map<String, Object>> orderLoglist = XmlUtil.setOrderList(ordList);
            // 将转换后的map添加到dataSetList中

            Map orderLogDataSet = XmlUtil.mapToMapForOrder(orderLoglist);
            dataSetList.add(orderLogDataSet);

            hasData = true;
        }
        // 结算日志信息处理
        List<PayLog> paList = payService.selectList();
        if (CollectionUtils.isNotEmpty(paList)) {
            List<Map<String, Object>> payLoglist = XmlUtil.setPayList(paList);
            // 将转换后的map添加到dataSetList中

            Map payLogDataSet = XmlUtil.mapToMapForPay(payLoglist);
            dataSetList.add(payLogDataSet);
            hasData = true;
        }
        // 行驶轨迹日志信息处理
        // 行驶轨迹日志DATASET中ITEM数据
        List<PositionLog> posiList = positionService.selectListForDistinct();
        if (CollectionUtils.isNotEmpty(posiList)) {
            List<Map<String, Object>> positionLoglist = XmlUtil.setPositionList(posiList);
            // 将转换后的map添加到dataSetList中

            Map positionLogDataSet = XmlUtil.mapToMapForDriving(positionLoglist);
            dataSetList.add(positionLogDataSet);

            hasData = true;
        }
        // 聊天日志
        //Map imLogDataSet = XmlUtil.mapToMapForIM();
        // dataSetList.add(imLogDataSet);

        // 快递日志
        //Map deliveryLogDataSet = XmlUtil.mapToMapForDelivery();
        //dataSetList.add(deliveryLogDataSet);


        // 插入十个日志表的数据
        dataSetMap.put("DATASET", dataSetList);
        if (hasData) {
            try {
                // 生成xml文件
                Document doc = XmlUtil.map2xml(dataSetMap, "MESSAGE");
                //String name = DateUtil.formatDate(new Date()).replaceAll("-","") + ".xml";
                String name = "91430100MA4M19Q18W-430104-" + System.currentTimeMillis() / 1000 + ".xml";
                String fileName = "/opt/push/" + name;
                XmlUtil.createXmlFile(fileName, doc);

                // 文件上传
                File file = new File(fileName);
                InputStream is = new FileInputStream(file);
                FtpUtil ftpUtils = FtpUtil.getInstance();
                ftpUtils.uploadFile("/", "kgzc", name, is);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }

    @Scheduled(cron = "0 0 0 * * ?") // 每天0时0分0秒清空数据库信息
    public void truncateTable() {
        //清空表数据
        try {
            customerService.deleteAll();
            driverService.deleteAll();
            loginService.deleteAll();
            orderService.deleteAll();
            payService.deleteAll();
            positionService.deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
